package com.aaa.qy127.service.impl;

import com.aaa.qy127.dao.TypeDao;

import com.aaa.qy127.entity.Type;
import com.aaa.qy127.service.TypeService;
import com.aaa.qy127.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("typeServiceImpl")
public class TypeServiceImpl implements TypeService {

    /*
    * 注入 TypeDao 接口
    * */
    @Resource(name = "typeDaoImpl")
    private TypeDao typeDao;

    /**
     * 查询所有的书籍类型 并返回
     * @return
     */
    @Override
    public List<Map<String, Object>> selType(){
        List<Map<String, Object>> allType = typeDao.getAllType();
        return allType;
    }

    @Override
    public Map<String, Object> delType(Type types) {
        int i = 0;
        Map<String, Object> map = new HashMap<>();
        if (types.getTypeid() != null) {
            if (types.getTcount() == 0) {
                i = typeDao.delType(types);
                if (i > 0) {
                    map.put("code","0001");
                }
            }else{
                map.put("code","0002");
            }
        }else{
            map.put("code","0003");
        }
        return map;
    }

    @Override
    public Result queryAllType(Type types) {
        Result resultEntity = new Result();
        resultEntity.setCount(typeDao.getAllCount());
        resultEntity.setData(typeDao.queryAllType(types));
        return resultEntity;
    }
    @Override
    public Map<String, Object> insertAndUpdate(Type types) {
        int i = 0;
        Map<String, Object> map = new HashMap<>();
        //调用方法查询前台输入的书籍类型名称是否已经存在
        List<Map<String, Object>> maps = typeDao.selTypename(types.getTypename());
        if (types.getTypeid() == null) {
            if("".equals(types.getTypename())) {
                //如果输入名称为空
                map.put("code","100");
            }else if (maps.size()!=0) {
                //如果输入名称已存在
                map.put("code","300");
            }else{
                i = typeDao.insert(types);
                if (i > 0) {
                    map.put("code","200");
                }
            }
        }else{
            if("".equals(types.getTypename())) {
                //如果输入名称为空
                map.put("code","100");
            }else if (maps.size()!=0) {
                //如果输入名称已存在
                map.put("code","300");
            }else{
                i = typeDao.updateType(types);
                if (i > 0) {
                    map.put("code","200");
                }
            }
        }
        if(i<0){
            map.put("code","000");
        }
        return map;
    }
}
